package com.mantru.iotpot.Tools;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.util.Log;
import android.view.ViewOutlineProvider;
import android.widget.ImageView;

import java.io.File;

public class ImageLoader {
    public static void loadImage(String imagePath, ImageView imageView) {
        // 检查图片路径是否有效
        if (imagePath == null || imagePath.isEmpty()) {
            Log.e("ImageLoader", "Image path is null or empty");
            return;
        }

        // 检查文件是否存在
        File imageFile = new File(imagePath);
        if (!imageFile.exists()) {
            Log.e("" +
                    "", "Image file does not exist: " + imagePath);
            return;
        }

        // 使用 BitmapFactory 加载图片
        Bitmap bitmap = BitmapFactory.decodeFile(imagePath);

        // 将 Bitmap 设置到 ImageView
        if (bitmap != null) {
            imageView.setImageBitmap(bitmap);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                // 关键代码：根据背景的圆角形状裁剪内容
                imageView.setOutlineProvider(ViewOutlineProvider.BACKGROUND);
                imageView.setClipToOutline(true);
            }
        } else {
            Log.e("ImageLoader", "Failed to decode bitmap from path: " + imagePath);
        }
    }
}
